<?php
/**
 * @version		$Id: hosting_acc.php 3 2011-12-16 20:19:15Z netix.mail@gmail.com $
 * @package		ALM Hoster
 * @desription	Web hosting and domain menager for resellers
 * @subpackage	Components
 * @copyright	Copyright (C) 2011 ALM Studio. All rights reserved.
 * @author		ALM Studio
 * @email		info@alm-studio.net
 * @link		http://alm-studio.net
 * @license		Commercial
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.modeladmin');

class AlmHosterModelHosting_acc extends JModelAdmin
{
	
	
	public function getTable($type = 'Hosting_accs', $prefix = 'AlmHosterTable', $config = array()) 
	{
		return JTable::getInstance($type, $prefix, $config);
	}
	
	
	public function getForm($data = array(), $loadData = true) 
	{
		$form = $this->loadForm('com_almhoster.hosting_acc', 'hosting_acc', array('control' => 'jform', 'load_data' => $loadData));
		if (empty($form)) 
		{
			return false;
		}
		return $form;
	}

	
	protected function loadFormData() 
	{

		$data = JFactory::getApplication()->getUserState('com_almhoster.edit.hosting_acc.data', array());
		if (empty($data)) 
		{
			$data = $this->getItem();
		}
		return $data;
	}
	
	
	public function save($data)
	{
		$pk	= (!empty($data['WhmaccId'])) ? $data['WhmaccId'] : (int)$this->getState('hosting_acc.WhmaccId');
		$isNew	= true;

		$table = $this->getTable();

		if ($pk > 0) {
			$table->load($pk);
			$isNew = false;
		}
		if (!$table->bind($data)) {
			$this->setError($table->getError());
			return false;
		}
		if (!$table->check()) {
			$this->setError($table->getError());
			return false;
		}
		if (!$table->store()) {
			$this->setError($table->getError());
			return false;
		}
		$this->setState('hosting_acc.WhmaccId', $table->WhmaccId);
		
		$db		= JFactory::getDbo();
		$query	= $db->getQuery(true);
		
		$query->select('*');
		$query->from('#__almh_whmaccounts');
		$query->where('WhmaccId = '.$table->WhmaccId);
		$db->setQuery($query);
		$db->setQuery((string) $query);	
		$data = $db->loadObject();
		
		if ($db->getErrorNum()) {
				JError::raiseWarning(500, $db->getErrorMsg());
				return false;
			}				
		$createacc = curldata($data->WhmaccServer,createacc($table->WhmaccId));			
		$app = JFactory::getApplication();
		$urlredirect = "index.php?option=com_almhoster&view=hosting_accs";
		$app->redirect( $urlredirect, $msg );
		
		return true;	
	}
}
